package com.evoupsight.monitorpass.gateway.handlers.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.ByteOrder;
import java.nio.charset.Charset;

public class MessageEncoder extends MessageToByteEncoder<Message> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
        // M
        out.writeByte(77);
        // O
        out.writeByte(79);
        // N
        out.writeByte(78);
        // D
        out.writeByte(68);
        // 1
        out.writeByte(1);
        out.writeLongLE(msg.getBody().length());
        if (null == msg.getBody()) {
            out.writeInt(0);
        } else {
            out.writeCharSequence(msg.getBody(), Charset.defaultCharset());
        }
    }
}
